<template>
  <el-dialog append-to-body title="案件延期" width="640px" top="10vh" :visible="visible" @close="handleClose">
    <el-form :model="form" :rules="rules" ref="form" class="white-card" label-width="110px">
      <!-- 同一个案件最多可延期三次，前两次最长延期时间30天，最后一次延期不限定时间。 -->
      <el-form-item label="延期次数：">第 {{ Number(addTimeNum) + 1 }} 次延期</el-form-item>
      <el-form-item label="延期天数：" prop="days">
        <div class="flex">
          <el-input v-model.number="form.days" placeholder="延期天数"></el-input>
          <span class="unit">天</span>
        </div>
      </el-form-item>
      <el-form-item label="延期情形：" prop="reason">
        <el-select v-model="form.reason" placeholder="请选择" style="width: 100%">
          <el-option v-for="item in optionList" :key="item.id" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="延期理由：" prop="comment">
        <el-input v-model="form.comment" show-word-limit maxlength="300" placeholder="请输入详细描述" type="textarea" style="width: 100%; height: 150px"></el-input>
      </el-form-item>
    </el-form>

    <div class="split"></div>

    <div class="dialog-footer-btn">
      <el-button @click="handleClose" type="primary" plain>关闭</el-button>
      <el-button @click="handleSubmit" :loading="submitting" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as workOrderApi from "@/api/workOrder/index";
import * as mixins from "@/utils/mixins";
import * as dictApi from "@/api/dict";
import * as templateApi from "@/api/template";

export default {
  name: "postponeDialog",
  mixins: [mixins.loading, mixins.form, mixins.dialog],
  props: ["visible", "workOrderNo", "addTimeNum", "type"],
  data() {
    return {
      submitting: false,
      form: {
        reason: "",
        days: "",
        comment: "",
      },
      rules: {
        reason: [{ required: true, message: "请选择" }],
        days: [
          { required: true, message: "请输入延期天数" },
          {
            validator: (rule, value, callback) => {
              if (!/^[0-9]*$/.test(value)) {
                callback(new Error("请输入正整数"));
                return;
              }

              if (value < 1) {
                callback(new Error("请输入大于0的整数"));
                return true;
              }

              if (value > 30 && this.addTimeNum + 1 !== 3) {
                callback(new Error("最多延期30个自然日，请重新确认"));
                return true;
              }
              callback();
            },
          },
        ],
        comment: [{ required: true, message: "请填写" }],
      },
      optionList: [],
    };
  },
  methods: {
    show() {
      dictApi.list({ parentCode: "1004" }).then((res) => {
        this.optionList = res.result;
      });

      templateApi.detailByCode(1008).then((res) => {
        this.form.comment = res.result.value;
      });
    },
    handleSubmit() {
      // if (this.form.days > 30) return this.$message.error("最多延期30个自然日，请重新确认");

      this.validateAndConfirm("form", "确定要延期该案件么？")
        .then(() => {
          this.submitting = true;
          return workOrderApi[this.type].addtime({
            ...this.form,
            workOrderNo: this.workOrderNo,
          });
        })
        .then(() => {
          this.$emit("success");
          this.handleClose();
        })
        .finally(() => (this.submitting = false));
    },
  },
};
</script>

<style lang="less" scoped>
.tips {
  text-align: center;
  margin-bottom: 20px;
}
.flex {
  display: flex;
  align-items: center;
  .unit {
    margin-left: 10px;
  }
}
</style>
